import os

import facade
import requests
import urllib3
from xjlibrary.mdatetime.mtime import MDateTime, getTodayDate
from xjlibrary.newthreading.MThreadingRun import MThreadingRun
from xjlibrary.newthreading.mthreading import ThreadManager
from xjlibrary.our_file_dir import BaseDir, ImageFile

curPath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curPath, -2)
dirPath = BaseDir.get_new_path(TopPath, "download", "TandBook", "download", "cover")
configfile = BaseDir.get_new_path(curPath, "db.ini")


class DownCover(object):

    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile,
                                             "db",
                                             logger=self.logger)
        self.HEADERS = {
            'Host': 'images.tandf.co.uk',
            'Referer': 'https://www.taylorfrancis.com/',
            'origin': 'https://www.taylorfrancis.com',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
        }
        self.set_session()

    def set_session(self):
        session = requests.Session()
        # requests.packages.urllib3.disable_warnings()
        # 帮助快速禁用urllib3的警告
        urllib3.disable_warnings()
        session.headers = self.HEADERS
        # 关闭ssl验证
        session.verify = False
        self.session = session

    # 获取DB3中不存在cover的更新任务
    def getNoCoverTask(self):
        update_time = MDateTime().get_day_sage(10) + " 00:00:00"
        Select_Sql = "SELECT isbn13, url, ava_url FROM cover WHERE  stat != 1 AND update_time > '{}'".format(
            update_time)
        result = self.mysqlutils.SelectFromDB(Select_Sql)
        return result

    # 获取封面保存路径
    def getCoverPath(self, isbn, url):
        dir_path = os.path.join(dirPath, isbn[:7])
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)
        cover_name = isbn + '.jpg'
        cover_path = os.path.join(dir_path, cover_name)
        self.logger.info('Save cover to <%s>' % cover_path)
        return cover_path

    # 根据URL下载封面
    def downloadCover(self, threadval, isbn, all_url, ava_url):
        """
        :param session: 会话连接
        :param isbn: 对应的isbn
        :param all_url: 所有的封面url字符串
        :param ava_url: 验证可用的封面url
        :return: 封面保存成功返回True,以及可用ava_url,反之为False
        """
        if ava_url:
            self.logger.info("ava_url is:" + ava_url)
            BoolResult, errString, r = facade.BaseRequest(url=ava_url,
                                                          sn=self.session,
                                                          endstring="",
                                                          timeout=(20, 30))
            if BoolResult:
                cover_path = self.getCoverPath(isbn=isbn, url=ava_url)
                ImageFile.img_to_format_jpg(r.content, cover_path)
                cover_field = '/smartlib/tandfbook/' + isbn[:7] + '/' + isbn + '.jpg'
                batch = getTodayDate() + '00'  # 设置批次
                Sql = "UPDATE allfields SET cover='%s', batch='%s' WHERE rawid='%s'" % (cover_field, batch, isbn)
                threadval.result_queue.put(Sql)
                Sql = "UPDATE cover SET ava_url='%s',stat=1 WHERE isbn13='%s'" % (ava_url, isbn)
                threadval.result_queue.put(Sql)
            else:
                Sql = "UPDATE cover SET failcount= failcount + 1 WHERE isbn13='%s'" % isbn
                threadval.result_queue.put(Sql)
        else:
            all_cover_list = all_url.split('**')
            for each_url in all_cover_list:
                BoolResult, errString, r = facade.BaseRequest(url=each_url,
                                                              sn=self.session,
                                                              endstring="",
                                                              timeout=(20, 30))
                if BoolResult:
                    cover_path = self.getCoverPath(isbn=isbn, url=each_url)
                    ImageFile.img_to_format_jpg(r.content, cover_path)
                    cover_field = '/smartlib/tandfbook/' + isbn[:7] + '/' + isbn + '.jpg'
                    batch = getTodayDate() + '00'  # 设置批次
                    Sql = "UPDATE allfields SET cover='%s', batch='%s' WHERE rawid='%s'" % (cover_field, batch, isbn)
                    threadval.result_queue.put(Sql)
                    Sql = "UPDATE cover SET ava_url='%s',stat=1 WHERE isbn13='%s'" % (each_url, isbn)
                    threadval.result_queue.put(Sql)
                else:
                    Sql = "UPDATE cover SET failcount= failcount + 1 WHERE isbn13='%s'" % isbn
                    threadval.result_queue.put(Sql)


class DownCoverThreadRun(MThreadingRun):

    def __init__(self, num):
        super(DownCoverThreadRun, self).__init__(num)
        self.downcover = DownCover()

    def getTask(self, *args, **kwargs):
        rows = self.downcover.getNoCoverTask()
        return rows

    def setTask(self, results=None, *args, **kwargs):
        for isbn13, url, ava_url in results:
            self.add_job(self.func, isbn13, url, ava_url)
        return "break"

    def setProxy(self, proxysList=None):
        return "break"

    def is_break(self):
        return True

    def thread_pool_hook(self, thread_pool_dicts: dict, dictsfull: dict, *args, **kwargs):
        if dictsfull["threadname"] in ["task_thread_1", "proxy_thread_1"]:
            thread = dictsfull["thread"]
            if not isinstance(thread, ThreadManager):
                raise TypeError("线程对象不是ThreadManager")
            thread.is_restart = False

    def fun(self, threadval, *args, **kwargs):
        self.downcover.downloadCover(threadval, args[0], args[1], args[2])

    def dealresult(self, *args, **kwargs):
        for sql in self.results:
            self.downcover.mysqlutils.ExeSqlToDB(sql)


def main():
    threadrun = DownCoverThreadRun(20)
    threadrun.run(model=1)


if __name__ == "__main__":
    main()
